﻿using System;
using System.Text;
using System.Xml; 
using System.ServiceModel;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DXTools.UI.Modules.Statistics.Model;
using DXTools.Core;
using DXTools.Core.CrmSdk;
using DXTools.UI.Core.ViewModel;
using DXTools.UI.Modules.Statistics.ViewModels;
using System.Collections.ObjectModel;

namespace DXTools.UI.Modules.Statistics.DataProviders
{
   
    public class CRMDataProvider : IDataProvider
    {
        Uri _server;

        public CRMDataProvider()
        {
            _server = new Uri(DXTools.Core.ClientScript.GetServerUrl());
        }


        public void RetrieveEntities(AsyncCallback asyncCallback)
        {
            try
            {
                DXTools.Core.CrmSdk.IOrganizationService service = DXTools.Core.WCFHelper.GetConnection(_server);

                OrganizationRequest request = new OrganizationRequest();
                request.RequestName = "RetrieveAllEntities";
                request.Parameters = new ParameterCollection();
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("EntityFilters", EntityFilters.Entity));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("RetrieveAsIfPublished", false));

                service.BeginExecute(request, asyncCallback, service);

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw ex;
            }
        }


        public void RetrieveEntityRecordCount(String LogicalName, String PrimaryIdAttribute, int FetchTotal, int PageNumber, AsyncCallback asyncCallback)
        {
            try
            {
                DXTools.Core.CrmSdk.IOrganizationService service = DXTools.Core.WCFHelper.GetConnection(_server);

                OrganizationRequest request = new OrganizationRequest();
//                string entityRecordCountQuery = @" 
//                <fetch distinct='false' mapping='logical' aggregate='true' page='" + PageNumber + "' count='" + FetchTotal + "'><entity name='" + LogicalName + "' ><attribute name='" + PrimaryIdAttribute + "' alias='recordcount' aggregate='count'/>" +
//                "</entity></fetch>";
                string entityRecordCountQuery = @" 
                <fetch distinct='false' mapping='logical' page='" + PageNumber + "' count='" + FetchTotal + "' ><entity name='" + LogicalName + "' ><attribute name='" + PrimaryIdAttribute + "' />" +
                "</entity></fetch>";
               
                service.BeginRetrieveMultiple(new FetchExpression() { Query = entityRecordCountQuery }, asyncCallback, service);

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw ex;
            }
        }

        

       
    }
}
